package com.example.administrator.zyfypt_421lff.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.zyfypt_421lff.R;
import com.example.administrator.zyfypt_421lff.bean.LoginBean;
import com.example.administrator.zyfypt_421lff.iface.LoginListener;
import com.example.administrator.zyfypt_421lff.model.LoginModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv;
    private TextView et;
    private TextView wc;
    private EditText user;
    private EditText pass;
    private Button button1;
    private Button button2;
    private Switch sw;
    private String username = "", password = "", sessionid = "";//用户名，密码
    private SharedPreferences sp;
    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            System.out.println("result:" + loginBean.getId());
            if (loginBean.getSessionid() != null) {
                sessionid = loginBean.getSessionid().toString();
                username=loginBean.getUsername();
                saveSP();
               Intent intent = new Intent(LoginActivity.this, MainActivity.class);
               startActivity(intent);
            }
        }


        @Override
        public void onFail(String msg) {
            System.out.println("fail");
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toast.makeText(this,"登录",Toast.LENGTH_SHORT).show();
        init();
        //实例化sp
        sp=getSharedPreferences("login",MODE_PRIVATE);
        //读取sp
        readSP();
    }
    private void readSP() {
        String name=sp.getString("username",null);
        String pass1=sp.getString("password",null);
        String session=sp.getString("sessionid",null);
        Boolean b=sp.getBoolean("remember",false);
        if(b){
            user.setText(name);
            pass.setText(pass1);
            sw.setChecked(b);
        }
    }
    private void init() {
        tv=(TextView)findViewById(R.id.textView);
        et =(TextView)findViewById(R.id.textView2);
        wc =(TextView)findViewById(R.id.textView6);
        user=(EditText)findViewById(R.id.editText6);
        pass=(EditText)findViewById(R.id.editText7);
        button1=(Button)findViewById(R.id.button);
        button2=(Button)findViewById(R.id.button2);
        sw=(Switch) findViewById(R.id.switch1);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                //startActivity(intent);
                LoginModel loginModel = new LoginModel();
                loginModel.getResult(user.getText().toString(), pass.getText().toString(), loginListener);
                saveSP();
                break;
            case R.id.button2:
               // Intent intent2 = new Intent(LoginActivity.this,RegisterActivity.class);
               // startActivity(intent2);
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);//forward为当前activity，forwardTarget为新的activity
                startActivity(intent);
                break;
            default:
                break;
        }
    }
        private void saveSP() {
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("username", username);
            editor.putString("password", password);
            editor.putString("sessionid", sessionid);
            editor.putBoolean("remember", sw.isChecked());
            editor.commit();//提交保存
        }
}
